import QtQuick 1.0
import "PlurkCore" 1.0 as Plurk
import "js/plurker.js" as DataModel

Rectangle {

  id: main
  state: plurkState
  width: parent.width
  height: parent.height

  property alias fontSize: view.fontSize

  Plurk.Loading {
    id: loading
    x: 0
    y: 0
    width: main.width
    height: main.height - toolbar.height
    text: "Connecting to Plurk";
    fontSize: main.fontSize
    anchors.verticalCenter: parent.verticalCenter;
    visible: false;
  }

  Plurk.PlurkView {
    id: view
    x: 0
    y: 0
    width: main.width
    height: main.height - toolbar.height
  }

  Plurk.ToolBar {
    id: toolbar
    x: 0
    y: main.height - toolbar.height
    width: main.width
  }

  Plurk.NewMsg {
    id: newmsg
    visible: false
    x: 0
    y: 0
    height: main.height - toolbar.height
    width: main.width
  }

  Connections {
    target: newmsg
    onPlurkSubmitted: {
      if (state == "newPlurk") {
        toolbar.refresh_rotating = true;
        state = "showPlurk";
        plurk.GetPlurks();
      } else if (state == "replyPlurk") {
        toolbar.refresh_rotating = true;
        state = "showPlurk";
        view.plurkDetailModel.clear();
        view.plurkDetailModel.append(view.plurkModel.get(view.modelIndex));
        plurk.GetResponses(view.currentPlurk);
      }
    }
    onPlurkSubmitting: {
      toolbar.refresh_rotating = true;
    }
  }

  Connections {
    target: toolbar
    onClick: {
      if (index == 0) {
        if (view.plurkView.state == "hidden") {
          toolbar.refresh_rotating = false;
          view.plurkView.state = "visible"
          plurk.AbortResponses();
          // plurk.GetPlurks();
        }
      }
      if (index == 2) {
        newmsg.visible = true;
        newmsg.editor.text = "";        
        if (view.plurkView.state == "visible") {
          state = "newPlurk";
          newmsg.editor.focus = true;
          newmsg.plurk_id = 0;
          newmsg.is_response = false;
        } else if (view.plurkView.state == "hidden") {
          state = "replyPlurk";
          newmsg.editor.focus = true;
          newmsg.plurk_id = view.currentPlurk;
          newmsg.is_response = true;
        }
        return;
      }
      if (index == 3) {
        if (view.plurkView.state == "visible") {
          toolbar.refresh_rotating = true;
          plurk.AbortResponses();
          plurk.GetPlurks();
        } else  if (view.plurkView.state == "hidden") {
          toolbar.refresh_rotating = true;
          plurk.AbortPlurks();
          plurk.GetResponses(view.currentPlurk);
        }
      }
      if (index == 4) {
        Qt.quit();
      }
    }
  }

  Connections {
    target: plurk
    onUnreadCount : {
      console.log(value);
      console.log(value["all"]);
    }
    onPlurksReceived : {
      if (state == "loading") state = "plurkInitOk";
      view.plurkView.state = "visible";
      var users = value.plurk_users;
      var plurk = value.plurks;
      view.plurkModel.clear();
      DataModel.handlePlurk(view.plurkModel, users, plurk, 0);
      toolbar.refresh_rotating = false;
    }
    onResponsesReceived : {
      var users = value.friends;
      var _plurk = value.responses;
      // plurkModel.clear();
      toolbar.refresh_rotating = false;
      DataModel.handlePlurk(view.plurkDetailModel, users, _plurk, 1);
    }
  }

  Connections {
      target: view
      onShowPlurkResponse : {
          toolbar.refresh_rotating = true;
          view.plurkDetailModel.clear();
          view.plurkDetailModel.append(view.plurkModel.get(index));
          plurk.GetResponses(plurk_id);
          view.plurkView.state = "hidden"
      }
  }

  states: [
    State {
      name: "loading";
      PropertyChanges { target: loading; visible: true; }
      PropertyChanges { target: view; visible: false; }
      PropertyChanges { target: newmsg; visible: false; }
      StateChangeScript { script: plurk.GetPlurks();
      }
    },
    State {
      name: "finishOAuth";
      PropertyChanges { target: loading; visible: false; }
      PropertyChanges { target: view; visible: true; }
      PropertyChanges { target: newmsg; visible: false; }
      StateChangeScript { script: plurk.GetPlurks();
      }
    },
    State {
      name: "plurkInitOk";
      PropertyChanges { target: loading; visible: false; }
      PropertyChanges { target: view; visible: true; }
      PropertyChanges { target: newmsg; visible: false; }
    },
    State {
      name: "newPlurk";
      PropertyChanges { target: loading; visible: false; }
      PropertyChanges { target: view; visible: false; }
      PropertyChanges { target: newmsg; visible: true; }
    },
    State {
      name: "replyPlurk";
      PropertyChanges { target: loading; visible: false; }
      PropertyChanges { target: view; visible: false; }
      PropertyChanges { target: newmsg; visible: true; }
    },
    State {
      name: "showPlurk";
      PropertyChanges { target: loading; visible: false; }
      PropertyChanges { target: view; visible: true; }
      PropertyChanges { target: newmsg; visible: false; }
    }
  ]
}
